package com.example.xianji.activity;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CustomProgressDialog;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Main_My_Receive_DetailActivity extends Activity implements OnClickListener {
    private ImageView my_receive_tiwen_details_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private TextView tiwen_details_question, tiwen_details_time, details_my_question;
    private Button button_details_send;
    private EditText edittext_my_question;
    private CustomProgressDialog mCustomProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main__my__receive__detail);
        ///	XJApplication.getInstance().addActivity(this);
        my_receive_tiwen_details_back = (ImageView) findViewById(R.id.my_receive_tiwen_details_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        tiwen_details_question = (TextView) findViewById(R.id.tiwen_details_question);
        tiwen_details_time = (TextView) findViewById(R.id.tiwen_details_time);
        button_details_send = (Button) findViewById(R.id.button_details_send);
        edittext_my_question = (EditText) findViewById(R.id.edittext_my_question);
        details_my_question = (TextView) findViewById(R.id.details_my_question);
        receive_details();
        my_receive_tiwen_details_back.setOnClickListener(this);
        button_details_send.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.my_receive_tiwen_details_back) {
            this.finish();
        } else if (v.getId() == R.id.button_details_send) {
            if (edittext_my_question.getText().toString().length() > 0) {
                mCustomProgressDialog = CustomProgressDialog.createDialog(this);
                mCustomProgressDialog.setCanceledOnTouchOutside(false);
                mCustomProgressDialog.show();
                send_my_question();
            } else {
                Toast.makeText(Main_My_Receive_DetailActivity.this, "请输入回答内容", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void receive_details() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("dqid", getIntent().getIntExtra("receive_id", 0));
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.receive_tiwen_details,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONObject object = response.getJSONObject("data");
                        tiwen_details_question.setText("服务方 :" + "  " + object.getString("query"));
                        tiwen_details_time.setText(object.getString("created_at"));
                        if (object.getInt("status") == 1) {
                            details_my_question.setVisibility(View.GONE);
                            edittext_my_question.setVisibility(View.VISIBLE);
                            button_details_send.setVisibility(View.VISIBLE);
                        } else if (object.getInt("status") == 2) {
                            details_my_question.setVisibility(View.VISIBLE);
                            details_my_question.setText("我：" + "  " + object.getString("detail"));
                            edittext_my_question.setVisibility(View.GONE);
                            button_details_send.setVisibility(View.GONE);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    public void send_my_question() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("dqid", getIntent().getIntExtra("receive_id", 0));
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        map.put("details", edittext_my_question.getText().toString());
        JSONObject params = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.sumbit_my_question,
                params, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    Toast.makeText(Main_My_Receive_DetailActivity.this, response.getString("msg"), Toast.LENGTH_SHORT).show();
                    if (response.getInt("status") == 1) {
                        Main_My_Receive_DetailActivity.this.finish();
                    }
                    if (mCustomProgressDialog != null) {
                        mCustomProgressDialog.dismiss();
                        mCustomProgressDialog = null;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }
}
